package com.x.client.activity

import android.support.v4.content.ContextCompat
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.gg.baselibrary.BaseActivity
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.gg.utils.AppManager
import com.gg.utils.StatusUtils
import com.gg.widget.LoadUtils
import com.gg.widget.navigationbar.DefaultNavigationBar
import com.lsxiao.apollo.core.Apollo
import com.x.client.R
import com.x.client.bean.City
import com.x.client.net.ApiFactory
import com.x.client.net.Composers
import com.x.client.utils.BusTag
import com.x.client.utils.RouterClass
import com.x.client.utils.RouterField
import kotlinx.android.synthetic.main.activity_recyclerview.*

/**
 *  Creator : GG
 *  Time    : 2017/12/1
 *  Mail    : gg.jin.yu@gmail.com
 *  Explain : 选择城市
 */
@Route(path = RouterClass.city)
class CityListActivity : BaseActivity() {

    private var adapter: CityAdapter = CityAdapter()

    @Autowired(name = RouterField.provinceId)
    @JvmField
    var mProvinceId: String = ""


    @Autowired(name = RouterField.cityId)
    @JvmField
    var mCityId: String = ""

    override fun getLayoutRes(): Int = R.layout.activity_recyclerview

    override fun initArguments() {
    }

    override fun initTitle() {
        StatusUtils.setStatusTextColor(true, this)
        StatusUtils.setStatusBar(this, false, false)
        DefaultNavigationBar.Builder(this)
                .setLeftIcon(R.drawable.ic_back_black)
                .setLeftClickListener (View.OnClickListener{ onBackPressed() })
                .setMiddleText("城市列表")
                .create()
    }

    override fun initView() {
    }

    override fun initData() {

        recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL).apply {
            setDrawable(ContextCompat.getDrawable(this@CityListActivity, R.drawable.divider_ll_grey_h)!!)
        })

        recyclerView.layoutManager = LinearLayoutManager(this)


        recyclerView.adapter = adapter

        adapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
            Apollo.emit(BusTag.city, adapter.data[position])
            AppManager.finishActivity(ProvinceActivity::class.java)
            AppManager.finishActivity()
        }

        LoadUtils.show(this)
        ApiFactory.getErpApi()
                .cityList(mProvinceId)
                .compose(Composers.handleError())
                .compose(this.bindToLifecycle())
                .subscribe({
                    it.forEach {
                        if (it.cityId == mCityId)
                            it.status = true
                    }
                    adapter.setNewData(it)
                   LoadUtils.hidden()
                }, {})

    }

    private class CityAdapter(data: List<City> = listOf()) : BaseQuickAdapter<City, BaseViewHolder>(R.layout.item_brand, data) {
        override fun convert(helper: BaseViewHolder, item: City) {
            helper.setGone(R.id.logo, false)
            helper.setText(R.id.name, item.cityName)
            if (item.status) {
                helper.setTextColor(R.id.name, ContextCompat.getColor(mContext, R.color.text_orange))
                helper.setVisible(R.id.check_mark, true)
            } else {
                helper.setTextColor(R.id.name, ContextCompat.getColor(mContext, R.color.text_black))
                helper.setVisible(R.id.check_mark, false)
            }
        }

    }
}